package com.hrt.manage.menus.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Cash;
import com.hrt.bean.Role;
import com.hrt.bean.UserVO;
import com.hrt.bean.UsersInfor;
import com.hrt.bean.ViewResource;
import com.hrt.bean.ViewResourceScopes;
import com.hrt.manage.menus.service.ViewResourceService;
import com.hrt.manage.sequence.service.SequenceService;
import com.hrt.util.PageUtils;

/**
 * 可访问资源控制器
 * 
 * @author Administor
 *
 */
@Controller
@RequestMapping("manage/viewResource")
public class ViewResourceController extends BaseController<ViewResource> {

	/**
	 * 0 后台 1 前台
	 */
	private Integer scope = 0;

	private ViewResource viewResource;

	private List<Map<String, Object>> scopeList;

	@Autowired
	private SequenceService seqService;

	@Autowired
	private ViewResourceService viewResourceService;

	/**
	 * 网站菜单列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/menuns/list")
	public ModelAndView listManageMenuns(
			@RequestParam(value = "scope", required = false) Integer scope) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("manage/viewResource/menuns/list");
		List<ViewResource> resultList = new ArrayList<ViewResource>();
		Map<String, Object> conditions = new HashMap<String, Object>();

		resultList = viewResourceService.queryByPage(scope,
				this.getPageIndex(), this.getPageSize());
		
		this.setTotalCount(viewResourceService.totalCount(scope));
		this.setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();
		// view.addObject("proTypes", getProTypes());
		mv.addObject("pageResult", page);
		mv.addObject("scope", scope);
		mv.addObject("scopeList", getScopeList());
		// view.addObject("productStatus", productStatus);
		// request.getSession().setAttribute("pageResult", page);

		return mv;
	}

	@RequestMapping(value = "/menuns/queryByPage")
	@ResponseBody
	public Map<String, PageUtils<ViewResource>> queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "scope", required = false) Integer scope,
			HttpServletRequest request) {
		Map<String, PageUtils<ViewResource>> resultMap = new HashMap<String, PageUtils<ViewResource>>();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		List<ViewResource> resultList = new ArrayList<ViewResource>();
		
		this.scope = scope;
		if (this.scope == null || this.scope == -1) {
			resultList = viewResourceService.queryByPage(getPageIndex(),
					getPageSize());
			setTotalCount(this.viewResourceService.totalCount(null));
		} else {
			resultList = viewResourceService.queryByPage(scope, pageIndex,
					pageSize);
			setTotalCount(this.viewResourceService.totalCount(scope));
		}
		setList(resultList);
		
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);

		return resultMap;
	}

	@RequestMapping(value = "/menuns/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request,
			HttpServletResponse response) {
		viewResource = new ViewResource();
		viewResource.setVrCode(seqService.getNextSequence(ViewResource.class));
		Map<String, Object> map = BaseController.transBean2Map(viewResource);

		return map;

	}

	@RequestMapping(value = "/menuns/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("menunDetail") ViewResource vresource,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		System.out.println("save");

		UserVO manager = this.getOnlineManager(request, response);

		vresource.setCreator(manager.getLoinName());
		vresource.setEditor(manager.getLoinName());
		vresource.setCreateTime(this.viewResourceService.getDate());
		vresource.setEditTime(this.viewResourceService.getDate());

		try {

			this.viewResourceService.saveMenun(vresource);

			response.sendRedirect("list.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/menuns/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("menunDetail") ViewResource vresource,
			HttpServletRequest request, HttpServletResponse response) {

		System.out.println("save");

		UserVO manager = this.getOnlineManager(request, response);
		if(manager!=null){
		ViewResource oldVrResource = this.viewResourceService
				.getViewResourceById(vresource.getId());
		
		oldVrResource.setEditor(manager.getLoinName());
		oldVrResource.setVrCode(vresource.getVrCode());
		oldVrResource.setVrName(vresource.getVrName());
		oldVrResource.setScope(vresource.getScope());
		oldVrResource.setNote(vresource.getNote());

		oldVrResource.setEditTime(this.viewResourceService.getDate());
		this.viewResourceService.updateRole(oldVrResource);
		}
		try {
			response.sendRedirect("list.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping(value = "/menuns/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) Long resourceId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (resourceId != null && !"".equals(resourceId)) {
			// Map resultMap=new HashMap<K, V>();
			System.out.println("showshowshowshowshow " + resourceId);
			viewResource = this.viewResourceService.getViewResourceById(resourceId);
			if (resourceId != null) {
				map = transBean2Map(viewResource);

			}

		}
		return map;

	}
	
	/**
	 * 删除菜单
	 * @param ids
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/menuns/delete", method = RequestMethod.POST)
	public @ResponseBody void delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			this.viewResourceService.deleteById(idArr);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}

	}

	public List<Map<String, Object>> getScopeList() {
		if (scopeList == null) {
			scopeList = ViewResourceScopes.getScopeList();
		}
		return scopeList;
	}
	
	

}
